package com.ayoka.parenting.widget;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.ayoka.parenting.R;
import com.jincp.common.brvah.BaseQuickAdapter;
import com.jincp.common.brvah.BaseViewHolder;
import com.jincp.common.brvah.RecycleViewDivider;
import com.jincp.common.dialog.BaseDialogBuilder;
import com.jincp.common.dialog.BaseDialogFragment;
import com.jincp.common.utils.UIUtils;
import com.jincp.filepicker.ui.image.ImagePickerManager;

import java.util.ArrayList;
import java.util.List;

/**
 * @ProjectName: AyokaClient.
 * @ClassName: PickItemDialog.
 * @Description: .
 * @Author: jincp.
 * @CreateDate: 2019/11/12 14:04.
 */

public class PickItemDialog extends BaseDialogFragment {

    public static PickItemDialog newInstance(PickItemDialog.Builder builder) {
        PickItemDialog dialog = new PickItemDialog();
        Bundle bundle = getArgumentBundle(builder);
        dialog.setArguments(bundle);
        return dialog;
    }

    @Override
    protected void convertView(@Nullable View container, final BaseDialogFragment dialogFragment) {
        RecyclerView recyclerView = container.findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        BaseQuickAdapter adapter = new BaseQuickAdapter<PickItem, BaseViewHolder>(R.layout.item_pick_item, pickItemList) {
            @Override
            protected void convert(BaseViewHolder holder, PickItem item) {
                holder.setText(R.id.tv_pick_item, item.getItemText())
                        .setTextColor(R.id.tv_pick_item, item.getItemTextColor());
            }
        };
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (mDialogResultListener != null){
                    mDialogResultListener.result(PickItemDialog.this, adapter.getItem(position));
                }
            }
        });
        recyclerView.setAdapter(adapter);
        recyclerView.addItemDecoration(RecycleViewDivider.getLineDivider(getActivity()));

        container.findViewById(R.id.tv_cancel).setOnClickListener(new onCloseClickListener());

    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_pick_item;
    }


    private List<PickItem> pickItemList;

    public void setPickItemList(List<PickItem> pickItemList) {
        this.pickItemList = pickItemList == null ? new ArrayList<PickItem>() : pickItemList;
    }


    public static class Builder extends BaseDialogBuilder<Builder, PickItemDialog> {

        @Override
        public PickItemDialog build() {
            return PickItemDialog.newInstance(this);
        }
    }

    /**
     * 点击右边关闭dialog监听
     */
    private class onCloseClickListener implements View.OnClickListener{

        @Override
        public void onClick(View view) {
            dismiss();
        }
    }

    public interface PickItem {
        String getItemText();
        int getItemTextColor();
        Object getItemValue();
    }

    public static List<PickItem> getPickPhotoItemList() {
        List<PickItem> result = new ArrayList<>();
        result.add(new PickItem() {
            @Override
            public String getItemText() {
                return UIUtils.getString(R.string.take_photo);
            }

            @Override
            public int getItemTextColor() {
                return UIUtils.getColor(R.color.red_darker);
            }

            @Override
            public Object getItemValue() {
                return ImagePickerManager.FROM_CAMERA;
            }
        });
        result.add(new PickItem() {
            @Override
            public String getItemText() {
                return UIUtils.getString(R.string.photo_graph);
            }

            @Override
            public int getItemTextColor() {
                return UIUtils.getColor(R.color.blue_darker);
            }

            @Override
            public Object getItemValue() {
                return ImagePickerManager.FROM_LOCAL;
            }
        });
        return result;
    }
}
